﻿using System.Windows;
using Microsoft.Practices.Prism.PubSubEvents;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Mitosoft.Definitions.Services;
using Prism.Common.Interfaces;
using Prism.Common.Services;
using Microsoft.Practices.Prism.Modularity;
using Prism.Services;
using Microsoft.Practices.Prism.Regions;
using Prism.Views;
using Prism.Common.RegionNames;

namespace Prism
{
  /// <summary>
  /// Class used to handle application boot tasks.
  /// </summary>
  public class Bootstrapper : UnityBootstrapper
  {
    /// <summary>
    /// Overrides creating the shell.
    /// </summary>
    /// <returns></returns>
    protected override DependencyObject CreateShell()
    {
      return ServiceLocator.Current.GetInstance<Shell>();
    }

    /// <summary>
    /// Overrides initializing the shell.
    /// </summary>
    protected override void InitializeShell()
    {
      Application.Current.MainWindow = (Window)Shell;
      Application.Current.MainWindow.Show();

	    // Dynamically choose the view being used in shell:
	    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
	    regionManager.RegisterViewWithRegion( 
		    RegionNames.MainShellRegionName,
		    typeof( TextFileReaderAppMainView ) );
    }

    /// <summary>
    /// Overrides creating the module catalog.
    /// </summary>
    /// <returns></returns>
    protected override IModuleCatalog CreateModuleCatalog()
    {
      return new ConfigurationModuleCatalog();
    }

    /// <summary>
    /// Overrides configuring the container.
    /// </summary>
    protected override void ConfigureContainer()
    {
      #region Good place to register application wide services

      //Needed if you want to use the event aggregator as a constructor
      //parameter of a service instance (below).
      //Then you have to use Container.Resolve<IEventAggregator> to get
      //an instance of the event aggregator:
      Container.RegisterType<IEventAggregator, EventAggregator>(
        new ContainerControlledLifetimeManager());

      Container.RegisterInstance<IApplicationManagerService>(
        new ApplicationManagerService(
          Container.Resolve<IEventAggregator>()));

      Container.RegisterInstance<IDirectoryService>(
        new DirectoryService());

      Container.RegisterInstance<IDispatcherService>(
        new UIDispatcher(Application.Current.Dispatcher));

      #endregion

      base.ConfigureContainer();
      
    }
  }
}
